import axios from 'axios'
import { baseURL } from '../configFile'
import DeviceData from '../types/DeviceData'

const axiosReq = axios.create({
    baseURL
})
//列出设备
export const listDevice = (offset: number, limit: number) => {
    return axiosReq.get('/listDevice', { params: { offset, limit } })
}

//根据用户id列出设备
export const listDeviceOnID = (offset: number, limit: number, userID: number) => {
    return axiosReq.get('/listDeviceOnID', { params: { offset, limit, userID } })
}

//查询设备数量
export const queryDeviceAmount = () => {
    return axiosReq.get('/queryDeviceAmount')
}

//根据用户ID查询设备数量
export const queryDeviceAmountOnID = () => {
    return axiosReq.get('/queryDeviceAmountOnID')
}

//搜索设备
export const searchDevice = (keyword?: string, deviceDataList?: Array<DeviceData>) => {
    return axiosReq.get('/searchDevice', { params: { keyword, deviceDataList } })
}

//获取设备封面
export const queryDeviceCover = (deviceID: number) => {
    return axiosReq.get('/queryDeviceCover', { params: { deviceID } })
}

//查询特定设备
export const queryDeviceInfo = (deviceID: number) => {
    return axiosReq.get('/queryDeviceInfo', { params: { deviceID } })
}

//获取设备图片
export const queryDevicePictures = (deviceID: number) => {
    return axiosReq.get('/queryDevicePictures', { params: { deviceID } })
}

//根据CategoryId获取DataCategory
export const queryCategory = (categoryID: number) => {
    return axiosReq.get('/queryCategory', { params: { categoryID } })
}

//超级管理员新增设备
export const supervisorAddDevice = (deviceName: string, deviceDataList: DeviceData[], manufacturerID: number, deviceAdminID?: number) => {
    return axiosReq.post('/supervisorAddDevice', { deviceName, manufacturerID, deviceDataList, deviceAdminID })
}

//设备管理员新增设备
export const deviceAdminAddDevice = (deviceName: string, deviceDataList: DeviceData[], manufacturerID: number) => {
    return axiosReq.post('/deviceAdminAddDevice', { deviceName, manufacturerID, deviceDataList })
}

//恢复已删除设备
export const deletedDeviceRecover = (deviceID: number) => {
    return axiosReq.post('/deletedDeviceRecover', { deviceID })
}

//列出已删除设备
export const listDeletedDevice = (offset: number, limit: number) => {
    return axiosReq.get('/listDeletedDevice', { params: { offset, limit } })
}

//查询已删除设备数量
export const queryDeletedDeviceAmount = () => {
    return axiosReq.get('/queryDeletedDeviceAmount')
}

//从已删除设备中根据关键词查询关键信息
export const searchDeletedDevice = (keyword: String) => {
    return axiosReq.get('/searchDeletedDevice', { params: { keyword } })
}

//上传二维数据
export const uploadTwoDimensionalData = (xlsxFile: File, deviceID: number, categoryID: number) => {
    const formData = new FormData();
    formData.append('xlsxFile', xlsxFile);
    formData.append('deviceID', deviceID.toString());
    formData.append('categoryID', categoryID.toString());
    return axiosReq.post('/uploadTwoDimensionalData', formData)
}

//上传附件检测
export const checkDeviceAttachment = (deviceID: number, fileNames: string[], fileSizes: number[]) => {
    return axiosReq.post('/checkDeviceAttachment', {
        'deviceID': deviceID,
        'fileNames': fileNames,
        'fileSizes': fileSizes
    })
}

//上传附件
export const addDeviceAttachment = (deviceID: number, files: File[], setAttachUploadProgress: (progress: any) => void) => {
    const formData = new FormData();
    formData.set('deviceID', deviceID.toString());
    files.forEach(item => {
        formData.append('files', item)
    })
    return axiosReq.post('/addDeviceAttachment', formData, { onUploadProgress: setAttachUploadProgress })
}

//上传图片检测
export const checkDevicePicture = (deviceID: number) => {
    return axiosReq.post('/checkDevicePicture', { 'deviceID': deviceID })
}

//上传图片
export const addDevicePicture = (deviceID: number, files: File[]) => {
    const formData = new FormData();
    formData.set('deviceID', deviceID.toString());
    files.forEach(item => {
        formData.append('files', item)
    })
    return axiosReq.post('/addDevicePicture', formData)
}

//删除设备
export const deleteDevice = (deviceID: number) => {
    return axiosReq.post('/deleteDevice', { 'deviceID': deviceID })
}

// //删除设备(不可恢复)
// export const destroyDevice = (deviceID: number) => {
//     return axiosReq.post('/destroyDevice', { 'deviceID': deviceID })
// }

//超管修改设备信息
export const supervisorModifyDevice = (deviceID: number, deviceName?: string, manufacturerID?: number, deviceDataList?: DeviceData[], deviceAdminID?: number) => {
    return axiosReq.post('/supervisorModifyDevice', {
        deviceID,
        deviceName,
        manufacturerID,
        deviceDataList,
        deviceAdminID
    })
}

//设备管理员修改设备信息
export const deviceAdminModifyDevice = (deviceID: number, deviceName?: string, manufacturerID?: number, deviceDataList?: DeviceData[]) => {
    return axiosReq.post('/deviceAdminModifyDevice', {
        deviceID,
        deviceName,
        manufacturerID,
        deviceDataList
    })
}

//删除设备已有类型
export const deleteDeviceData = (deviceID: number, dataCategoryID: number) => {
    return axiosReq.post('/deleteDeviceData', { deviceID, dataCategoryID })
}

//获取设备附件
export const queryAttachment = (deviceID: number) => {
    return axiosReq.get('/queryAttachment', { params: { deviceID } })
}

//删除设备附件
export const deleteDeviceAttachment = (deviceID: number, fileID: number[]) => {
    return axiosReq.post('/deleteDeviceAttachment', { deviceID, fileID })
}

//删除设备图片
export const deleteDevicePicture = (deviceID: number, pictureID: number[]) => {
    return axiosReq.post('/deleteDevicePicture', { deviceID, pictureID })
}


//设置设备封面
export const selectDeviceCover = (deviceID: number, pictureID: number) => {
    return axiosReq.post('/selectDeviceCover', { deviceID, pictureID })
}

//上传3D图片检测
export const checkDeviceStereoPicture = (deviceID: number, fileNames: string[], fileSizes: number[]) => {
    return axiosReq.post('/checkDeviceStereoPicture', {
        'deviceID': deviceID,
        'fileNames': fileNames,
        'fileSizes': fileSizes
    })
}

//添加/更新设备3D图片
export const addDeviceStereoPicture = (deviceID: number, files: File[], setUploadBar: (progress: any) => void) => {
    const formData = new FormData();
    formData.set('deviceID', deviceID.toString());
    files.forEach(item => {
        formData.append('files', item)
    })
    return axiosReq.post('/addDeviceStereoPicture', formData, { onUploadProgress: setUploadBar })
}

//请求设备3D图片
export const queryDeviceStereoPicture = (deviceID: number) => {
    return axiosReq.get('/queryDeviceStereoPicture', { params: { deviceID } })
}

//删除设备3D图片
export const deleteDeviceStereoPicture = (deviceID: number) => {
    return axiosReq.post('/deleteDeviceStereoPicture', { deviceID })
}

//根据用户id搜索设备
export const searchDeviceOnID = (keyword: string) => {
    return axiosReq.get('/searchDeviceOnID', { params: { keyword } })
}